package com.example.demo.controller;

import com.example.demo.model.Blog;
import com.example.demo.model.User;
import com.example.demo.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.HashMap;

@RestController
public class DetailsController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/details")
    public Object ShowBlog(Integer blogId, @SessionAttribute(value = "user",required = false) User user) {
        HashMap<String,Object> map = new HashMap<>();
        if (blogId == null || blogId < 1) {
            map.put("message","blogId异常!");
            return map;
        }
        Blog blog = blogService.getBlogByBid(blogId);
        if (blog == null) {
            map.put("message","不存在当前blogId的文章");
            return map;
        }
        blog.setIsAuthor(user.getUserId()==blog.getUserId()?1:0);
        return blog;
    }
}
